﻿module Sustainalytics.EsgRatings {

    import LRTM = Sustainalytics.LRTM;

    export class TestESGRatingsController {

        public $scope: ng.IScope;
        public UI: TestESGRatingUi;

        private notificationFactory: INotificationFactory;
        private $q: ng.IQService;
        private backend: TestingESGBackEndService;
        private longRunningTaskManagerService: LRTM.LongRunningTaskManagerService;
        
        constructor($scope: ng.IScope, $rootScope, $q: ng.IQService, $location: ng.ILocationService, $http: ng.IHttpService,
            TestingESGBackEndService, LongRunningTaskManagerService, notificationFactory: INotificationFactory, staticObjects) {

            var ctx = this;
            ctx.$q = $q;
            ctx.$scope = $scope;            
            ctx.backend = TestingESGBackEndService;
            ctx.longRunningTaskManagerService = LongRunningTaskManagerService;
            ctx.notificationFactory = notificationFactory;

            ctx.initialize();

            $scope['testESGratings'] = this;
        }

        public requestDownload() {
            var ctx = this;

            // check if the form is valid
            var ctx = this;
            if (ctx.$scope['testESGRatingsForm'].$invalid) {
                ctx.notificationFactory.error('', "Invalid selection, please select at least one element in each list");
                return;
            }

            var taskName = "ESGRatingAdminCheckReport " + moment().format("YYYY-MM-DD_HH-mm-ss-SSS");
            // preserve only required 
            var params =  {
                SelectedCompaniesIds: ctx.UI.SelectedCompaniesIds,
                SelectedPeerGroupsIds: ctx.UI.SelectedPeerGroupsIds,
                SelectedIndustryGroupsIds: ctx.UI.SelectedIndustryGroupsIds,
                SelectedMatrixId: ctx.UI.SelectedMatrixId,
                SelectedReferenceUniverseId: ctx.UI.SelectedReferenceUniverseId,
                SelectedComparativeGroup: ctx.UI.SelectedComparativeGroup,
                SelectedTemplateType: ctx.UI.SelectedTemplateType,
                SelectedCompanyType: ctx.UI.SelectedCompanyType,
                FileName: taskName,
                AllCompanies: ctx.UI.AllCompanies,
            };

            this.longRunningTaskManagerService.runTask(taskName,
                JSON.stringify(params),
                LRTM.LongRunningTaskTypeEnum.ESGRatingAdminCheckReport, true, null);
            
          
        }

      
        private initialize(): ng.IPromise<any> {

            var ctx = this;
         
            ctx.backend
                .getTestESGSetUpData()
                .then(
                (response => { ctx.UI = response.data.result; ctx.setDefaults(); }),
                (errorReason => { console.log(errorReason); })
                );

            return ctx.$q.defer().promise;
        }
        
        private setDefaults() {
            var ctx = this;

            ctx.UI.SelectedCompanyType = 2;
            ctx.UI.SelectedTemplateType = 2;

            var obj = ctx.UI.listWeightMatrixes.filter(obj => obj.name == "Default Weight Matrix");
            ctx.UI.SelectedMatrixId = obj[0].id;
            ctx.UI.SelectedComparativeGroup = 0;

        }
    
    }

    // register controller with EsgRatings Angular module
    angular.module('Sustainalytics.EsgRatings')
        .controller('TestESGRatingsController', TestESGRatingsController);
} 